﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DesignPatternDemoCApp.Behavioral.ObserverPattern
{
    /// <summary>
    /// 微信服务端:具体被观察者
    /// </summary>
    public class WechatServer : AbstractSubject
    {
        private IList<AbstractObserver> list;

        public WechatServer()
        {
            list = new List<AbstractObserver>();
        }

        public override void notifyObserver(string s)
        {
            //消息更新，通知所有观察者
            Console.WriteLine("微信服务更新消息： " + s);
            foreach (var item in list)
            {
                item.update(s);
            }
        }

        public override void registerObserver(AbstractObserver o)
        {
            if (!list.Contains(o))
            {
                list.Add(o);
            }
        }

        public override void removeObserver(AbstractObserver o)
        {
            if (list.Contains(o))
            {
                list.Remove(o);
            }
        }
    }
}
